const { exec } = require('../db/index')

const getBlogsList = async () => {
  const sql = `select * from blogs;`
  return await exec(sql)
}

const addBlogsItem = async ({title, content, author}) => {
  const sql = `insert into blogs (title, content, creatime, author) values ('${title}', '${content}' , ${Date.now()}, '${author}');`
  return await exec(sql)
}

const updateBlogsItem = async ({id, title, content, author}) => {
  const sql = `update blogs set title='${title}', content='${content}', creatime=${Date.now()},author='${author}' where id = ${id};`
  return await exec(sql)
}

const deleteBlogsItem = async (id) => {
  const sql = `delete from blogs where id=${id};`
  return await exec(sql)
}

module.exports = {
  getBlogsList,
  addBlogsItem,
  updateBlogsItem,
  deleteBlogsItem
}